package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* LogoView
* */
class LogoView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.STROKE
            strokeCap = Paint.Cap.SQUARE
            color = context.resources.getColor(R.color.color_FFD740)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    private val baseAngle = 150F
    private val offsetAngle = 0F

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy + (cy - cy * 11 / 12 * Math.cos(baseAngle.toDouble()).toFloat()) / 2)

            val mStrokeWidth = (cx / 6).coerceAtLeast(minStrokeWidth)
            mPaint0.strokeWidth = mStrokeWidth

            mPath0.apply {
                reset()
                arcTo(
                    -cx * 11 / 12,
                    -cy * 11 / 12,
                    cx * 11 / 12,
                    cy * 11 / 12,
                    150F - offsetAngle,
                    240F + offsetAngle,
                    false
                )
                moveTo(-cx * 11 / 12, 0F)
                quadTo(0F, -cy * 1 / 2, cx * 11 / 12, 0F)
            }
            drawPath(mPath0, mPaint0)

            restore()
        }
    }
}